package cn.itcast.n8;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

/**
 * className: TestWordCount
 * description:
 * date: 2023/3/7 0007-17:37
 * <p>
 * project: concurrent
 * package: cn.itcast.n8
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class TestWordCount {
    public static void main(String[] args) {
        demo(
                // 创建 map 集合
                // 创建 ConcurrentHashMap 对不对?
                // key value
                // a   200
                // b   200
                () -> new ConcurrentHashMap<String, LongAdder>(),
                (map, words) -> {
                    for (String word : words) {

                        // 如果缺少一个 key, 则计算生成一个 value,然后将 key value 放入 map
                        //                  a       1
                        LongAdder value = map.computeIfAbsent(word, key -> new LongAdder());
                        // 执行累加
                        value.increment();

                        // // 检查 key 有没有
                        // Integer counter = map.get(word);
                        // int newValue = counter == null ? 1 : counter + 1;
                        // // 没有 则 put
                        // map.put(word, newValue);
                    }
                }
        );
    }

    private static <V> void demo(Supplier<Map<String, V>> supplier, BiConsumer<Map<String, V>, List<String>> consumer) {
        Map<String, V> counterMap = supplier.get();
        // key value
        // a   200
        // b   200
        List<Thread> ts = new ArrayList<>();
        for (int i = 1; i <= 26; i++) {
            int idx = i;
            Thread thread = new Thread(() -> {
                List<String> words = readFromFile(idx);
                consumer.accept(counterMap, words);
            });
            ts.add(thread);
        }

        ts.forEach(Thread::start);
        ts.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println(counterMap);
    }

    private static List<String> readFromFile(int i) {
        List<String> words = new ArrayList<>();
        try (BufferedReader in = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get("tmp/" + i + ".txt"))))) {
            while (true) {
                String word = in.readLine();
                if (word == null) {
                    break;
                }
                words.add(word);
            }
            return words;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

